﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.Data.SqlClient;

public partial class Admin_UserControl_ucCustomer : System.Web.UI.UserControl
{
    public static int index;
    private static string ASCENDING = " ASC";
    private static string DESCENDING = " DESC";
    private static string sortExpression = null;
    private static string direction = null;
    TblEmailDAO EmailDAO = new TblEmailDAO(Global.CONNECTION_STRING);
    TblCustomerDAO CustomersDAO = new TblCustomerDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateAlphaPagings();
            this.ViewState["SelectedText"] = "All";
            LoadListEmail();
            LoadDetails();
        }
    }
    private void LoadDetails()
    {
        string idCus = Request.QueryString["idCus"];

        DataTable dtCus = CustomersDAO.GetCustomerById(Convert.ToInt32(idCus));
        foreach (DataRow dr in dtCus.Rows)
        {
            txtFName.Text = dr["FName"].ToString();
            txtLName.Text = dr["LName"].ToString();
            txtAddress.Text = dr["CusAddress"].ToString();
            txtMobilePhone.Text = dr["MobilePhone"].ToString();
            txtHomPhone.Text = dr["HomePhone"].ToString();
            txtWorkPhone.Text = dr["WorkPhone"].ToString();
        }
    }
    private void LoadListEmail()
    {
        BindGrid(ViewState["SelectedText"].ToString());
    }

    #region Sorting......

    private void BindGrid(string StartAlpha)
    {
        string idCus = Request.QueryString["idCus"];
        DataTable dt_dept = EmailDAO.Get_AllEmailByCusID(StartAlpha, 3, Convert.ToInt32(idCus));
        DataView dv = new DataView(dt_dept); // dt_dept là table ch?a d? li?u c?a gridview
        dv.Sort = sortExpression + direction;
        grdListEmail.DataSource = dv;
        grdListEmail.DataBind();

    }
    protected void grdListEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdListEmail.PageIndex = e.NewPageIndex;
        //LoadDriver();
        BindGrid(ViewState["SelectedText"].ToString());
    }
    private SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    private void SortGridView(string sortExpression, string direction, string alphabet)
    {
        string idCus = Request.QueryString["idCus"];
        DataTable dt_dept = EmailDAO.Get_AllEmailByCusID(alphabet, 1, Convert.ToInt32(idCus));
        DataView dv = new DataView(dt_dept); // dt_dept là table ch?a d? li?u c?a gridview
        dv.Sort = sortExpression + direction;
        grdListEmail.DataSource = dv;
        grdListEmail.DataBind();
    }
    protected void grdListEmail_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        ViewState["SortExpression"] = sortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING, ViewState["SelectedText"].ToString());
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING, ViewState["SelectedText"].ToString());
        }
    }

    #endregion


    #region Alphabet (tim kiem theo ten)

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if (this.ViewState["SelectedText"] != null)
            {
                LinkButton lbkbtnPaging = (LinkButton)e.Item.FindControl("lnkbtnPaging");
                if (this.ViewState["SelectedText"].ToString() == lbkbtnPaging.Text)
                    lbkbtnPaging.Enabled = false;
            }
        }
    }


    private void CreateAlphaPagings()
    {
        DataTable dt = new DataTable();

        if (this.ViewState["Paging"] == null)
        {
            SqlDataAdapter da = new SqlDataAdapter("sp_Proc_Paging", Global.CONNECTION_STRING);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("@OptionId", SqlDbType.Int).Value = 1;
            da.Fill(dt);
            this.ViewState["Paging"] = dt;
        }
        else
            dt = (DataTable)this.ViewState["Paging"];


        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton lbkbtnPaging = (LinkButton)e.CommandSource;
        BindGrid(e.CommandArgument.ToString());
        this.ViewState["SelectedText"] = e.CommandArgument.ToString();
        CreateAlphaPagings();
    }
    #endregion
    protected void grdListEmail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        //button.CommandArgument = ((TblEmail)e.Row.DataItem).IDEmail.ToString();
                        button.OnClientClick = "if (!confirm('Are you sure to delete this Email?')) return false;";
                    }
                }
            }
            e.Row.Cells[2].Text = Convert.ToDateTime(e.Row.Cells[2].Text).ToString("dd/MM/yyyy");

            //HyperLink hypName = (HyperLink)e.Row.Cells[3].FindControl("hypName");
            //hypName.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FName")) + " " + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "LName"));
            //hypName.NavigateUrl = "~/Admin/Default.aspx?id=" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CusID"));

            TextBox txtNotes = (TextBox)e.Row.Cells[3].FindControl("txtNotes");
            CheckBox ckEnable = (CheckBox)e.Row.Cells[3].FindControl("ckEnable");
            txtNotes.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Note"));
            if (ckEnable.Checked == true)
            {
                txtNotes.Enabled = true;
            }
            else
                txtNotes.Enabled = false;
        }
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        bool flag = false;
        try
        {
            TblEmail Email = new TblEmail();
            int idEmail = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            foreach (GridViewRow grd in grdListEmail.Rows)
            {
                CheckBox ckEnable = (CheckBox)grd.FindControl("ckEnable");
                if (ckEnable.Checked == true)
                {
                    TextBox txtNotes = (TextBox)grd.FindControl("txtNotes");
                    EmailDAO.UpdateNotes(idEmail, txtNotes.Text);
                }
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_LOGGED_ADMIN] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
            Response.Redirect("~/Admin/ListEmailActive.aspx");

    }

    protected void ckEnable_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow grd in grdListEmail.Rows)
        {
            TextBox txtNotes = (TextBox)grd.FindControl("txtNotes");
            CheckBox ckEnable = (CheckBox)grd.FindControl("ckEnable");

            if (ckEnable.Checked == true)
            {
                txtNotes.Enabled = true;
            }
            else
                txtNotes.Enabled = false;
        }
    }
}
